package com.baihan.angus.app.game.common.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.scwang.smart.drawable.ProgressDrawable;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.scwang.smart.refresh.layout.util.SmartUtil;

public class SmartRefreshHeader extends FrameLayout implements RefreshHeader {

    private ImageView mAnimImageView;
    private ProgressDrawable mProgressDrawable;

    public SmartRefreshHeader(@NonNull Context context) {
        super(context);
        init(context);
    }

    public SmartRefreshHeader(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context){
        mAnimImageView = new ImageView(context);
        mProgressDrawable = new ProgressDrawable();
        mProgressDrawable.setColor(0xffE6E6E6);
        mAnimImageView.setImageDrawable(mProgressDrawable);

        LayoutParams layoutParams = new  LayoutParams(SmartUtil.dp2px(20), SmartUtil.dp2px(20));
        layoutParams.gravity = Gravity.CENTER;
        addView(mAnimImageView, layoutParams);
        setMinimumHeight(SmartUtil.dp2px(60));
    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {
    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        if (mProgressDrawable.isRunning()){
            mProgressDrawable.stop();
        }
        return 0;
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        switch (newState) {
            case None:
            case PullDownToRefresh:
            case ReleaseToRefresh:
                break;
            case Refreshing:
                mProgressDrawable.start();
                break;
        }
    }
}
